// pages/winning/winning.js
var app = getApp();

Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		images: {},
		prizeInfo: {},
		prizeUser: {},
		imgUrl: app.urlimg,
		goods_lists: [],
	},

	/**
	 * 查看订单详情按钮事件
	 */
	getOrderInfo: function (e) {
		var orderId = e.currentTarget.dataset.order_id;
		wx.navigateTo({
			url: '/pages/mygroup/order/order?orderId=' + orderId,
		});
	},
	// 中奖商品详情
	goods_click: function () {
		var that = this;
		wx.redirectTo({
			url: '/pages/group/groupdetail/index?id=' + that.data.prizeInfo.goods_id + '& regiment_id=' + that.data.prizeInfo.regiment_id + '& group_type=' + that.data.prizeInfo.action_type,
		})
	},
	// 为你推荐列表
	recommend_click: function (e) {
		var that = this;
		console.log(e.currentTarget.dataset);
		wx.redirectTo({
			url: '/pages/group/groupdetail/index?id=' + e.currentTarget.dataset.goods_id + '& regiment_id=' + e.currentTarget.dataset.regiment_id + '& group_type=' + e.currentTarget.dataset.group_type,
		})
	},
	// 进入商品详情页面(去开团)
	click: function (e) {
		console.log(e.currentTarget);
		wx.redirectTo({
			url: '/pages/index/detail/index?id=' + e.currentTarget.dataset.index,
		})
	},
	// 广告图片大小（banner）
	imageLoad: function (e) {
		var $width = e.detail.width,    //获取图片真实宽度
			$height = e.detail.height,
			ratio = $width / $height;    //图片的真实宽高比例
		var viewWidth = 718,           //设置图片显示宽度，左右留有16rpx边距
			viewHeight = 718 / ratio;    //计算的高度值
		var image = this.data.images;
		//将图片的datadata-index作为image对象的key,然后存储图片的宽高值
		image[e.target.dataset.index] = {
			width: viewWidth,
			height: viewHeight
		}
		this.setData({
			images: image
		})
	},
	// 广告图片（banner）
	swiperItem: function (e) {
		var fid = e.currentTarget.id
		if (e.currentTarget.dataset.url !== '') {
			wx.redirectTo({
				url: e.currentTarget.dataset.url,
			})
		}
	},
	//进入首页
	homeTAP: function () {
		wx.reLaunch({
			url: '/pages/index/index',
		})
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {

		var that = this;

		var token_key = wx.getStorageSync('token_key');
		console.log(token_key);
		if (token_key == undefined || token_key == '') {
			app.getUserInfo(function () {
				load_fn();
			})
		} else {
			load_fn()
		}


		function load_fn() {
			//中奖详情
			wx.request({
				url: app.url + "api_team/winning",
				method: "GET",
				header: {
					'content-type': 'application/json',
					'token': wx.getStorageSync('token_key')
				},
				data: {
					orderId: options.orderId
				},
				success: function (res) {
					console.log(res);
					// 伪数据
					// res.data.result.prizeInfo.isPrize = 1;
					// res.data.result.prizeInfo.prize = 2;
					// res.data.result.prizeUsers = [{ head_ico: '../../../images/u213.png', username: '小墨、', order_no: '123456789123456789123' }];
					if (res.data.result.prizeUsers) {
						for (var i = 0; i < res.data.result.prizeUsers.length; i++) {
							var name = '';
							var order_num = '';
							//用户名称处理
							for (var name_index = 0; name_index < res.data.result.prizeUsers[i].username.length; name_index++) {
								if (name_index == 0) {
									name += res.data.result.prizeUsers[i].username[name_index];
								} else {
									name += '*';
								}
							}
							//用户订单号处理
							for (var order_index = 0; order_index < res.data.result.prizeUsers[i].order_no.length; order_index++) {
								if (order_index < res.data.result.prizeUsers[i].order_no.length - 4) {
									order_num += res.data.result.prizeUsers[i].order_no[order_index];
								} else {
									order_num += '*';
								}
							}
							res.data.result.prizeUsers[i].username = name;
							res.data.result.prizeUsers[i].order_no = order_num;
						}
					}
					that.setData({
						prizeInfo: res.data.result.prizeInfo,
						prizeUser: res.data.result.prizeUsers,
					});
					console.log(that.data.prizeUser);
				}
			});

			//拼团推荐
			wx.request({
				url: app.url + 'api_team/recommend',
				data: {
					// goods_id: o.id,
					goods_id: 2829,
				},
				header: {
					'Content-Type': 'application/json',
					'token': wx.getStorageSync('token_key')
				},
				success: function (res) {
					console.log(res.data.result);
					that.setData({
						goods_lists: res.data.result,
					})
				},
				// 接口调用失败返回的函数
				fail: function (err) {
					console.log(err)
				}
			})
		}
	},
	//查看优惠劵
	navTocoupon: function () {
		wx.navigateTo({
			url: '/pages/account/discount/index',
		})
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	}
})